<?php

class Project
{

	public static function find($arr = array())
	{
		global $db;

		if (empty($arr))
		{
			$st = $db->prepare("SELECT * FROM project");
		} else if ($arr['pid'])
		{
			$st = $db->prepare("SELECT * FROM project WHERE `pid`=:pid");
		} else if (isset($arr['pt']))
		{
			$st = $db->prepare("SELECT * FROM project WHERE `pt`=:pt ORDER BY `code` DESC LIMIT 1");
		} else
		{
			throw new Exception("Unsupported property!");
		}
		$st->execute($arr);
		$a = $st->fetchAll(PDO::FETCH_CLASS, "Project");
		return $a;
	}

	public static function add($arr)
	{
		global $db;
		$t = time();
		$arr['starttime'] = date("Y/m/d", $t);
		$arr['endtime'] = date("Y/m/d", $t);
		$arr['loading'] = 0;
		if ($arr['pt'] && $arr['code'] && $arr['project'])
		{
			$st = $db->prepare("INSERT INTO project (`pt`, `code`, `project`,`starttime`,`endtime`,`loading`) VALUES (:pt, :code, :project, :starttime, :endtime, :loading);");
		} else
		{
			throw new Exception("Unsupported property!");
		}
		$a = $st->execute($arr);
		return $a; //返回bool
	}

	public static function update($arr)
	{
		global $db;
		if ($arr['id'] && $arr['fd'] == "project" && $arr['val'])
		{
			$sql = "UPDATE project SET `project`='" . $arr['val'] . "' WHERE `pid`='" . $arr['id'] . "'";
			$st = $db->exec($sql);
		} elseif ($arr['id'] && $arr['fd'] == "master" && $arr['val'])
		{
			$sql = "UPDATE project SET `master`='" . $arr['val'] . "' WHERE `pid`='" . $arr['id'] . "'";
			$st = $db->exec($sql);
		} elseif ($arr['id'] && $arr['fd'] == "php" && $arr['val'])
		{
			$sql = "UPDATE project SET `php`='" . $arr['val'] . "' WHERE `pid`='" . $arr['id'] . "'";
			$st = $db->exec($sql);
		} elseif ($arr['id'] && $arr['fd'] == "as" && $arr['val'])
		{
			$sql = "UPDATE project SET `as`='" . $arr['val'] . "' WHERE `pid`='" . $arr['id'] . "'";
			$st = $db->exec($sql);
		} elseif ($arr['id'] && $arr['fd'] == "design" && $arr['val'])
		{
			$sql = "UPDATE project SET `design`='" . $arr['val'] . "' WHERE `pid`='" . $arr['id'] . "'";
			$st = $db->exec($sql);
		} elseif ($arr['id'] && $arr['fd'] == "test" && $arr['val'])
		{
			$sql = "UPDATE project SET `test`='" . $arr['val'] . "' WHERE `pid`='" . $arr['id'] . "'";
			$st = $db->exec($sql);
		} elseif ($arr['id'] && $arr['fd'] == "starttime" && $arr['val'])
		{
			$sql = "UPDATE project SET `starttime`='" . $arr['val'] . "' WHERE `pid`='" . $arr['id'] . "'";
			$st = $db->exec($sql);
		} elseif ($arr['id'] && $arr['fd'] == "endtime" && $arr['val'])
		{
			$sql = "UPDATE project SET `endtime`='" . $arr['val'] . "' WHERE `pid`='" . $arr['id'] . "'";
			$st = $db->exec($sql);
		} elseif ($arr['pid'] && $arr['fd'] == "loading")
		{
			$sql = "UPDATE project SET `loading`='" . $arr['val'] . "' WHERE `pid`='" . $arr['pid'] . "'";
			$st = $db->exec($sql);
		} else
		{
			throw new Exception("Unsupported property!");
		}
//		$st->execu($arr);
		if ($st)
			return TRUE;
	}

	public static function del($arr)
	{
		global $db;
//		Delete From Student  where [studentID] =1
		if ($arr['pid'])
		{
			$st = $db->prepare("DELETE FROM project  WHERE `pid`=:pid;");
		} else
		{
			throw new Exception("Unsupported property!");
		}
		$a = $st->execute($arr);
		return $a; //返回bool
	}

	public static function find_view($arr = array())
	{
		global $db;

		if (isset($arr['pt']))
		{
			$st = $db->prepare("SELECT * FROM project WHERE `pt`=:pt ORDER BY `code` DESC");
		} else
		{
			throw new Exception("Unsupported property!");
		}
		$st->execute($arr);
		$a = $st->fetchAll(PDO::FETCH_CLASS, "Project");
		return $a;
	}

	public static function find_week($arr = array())
	{
		global $db;

		$year = date('Y');
		$month = date('m');
		$day = date('d');
		$arr['days'] = date('Y/m/d', mktime(0, 0, 0, $month, $day - date('N'), $year));
		$arr['daye'] = date('Y/m/d', mktime(23, 59, 59, $month, $day - date('N') + 7, $year));

		if (isset($arr['pt']))
		{
			$st = $db->prepare("SELECT * FROM project WHERE `pt`=:pt AND `starttime` >= :days AND `starttime` <= :daye ORDER BY `code` ASC");
		} else
		{
			throw new Exception("Unsupported property!");
		}
		$st->execute($arr);
		$a = $st->fetchAll(PDO::FETCH_CLASS, "Project");
		return $a;
	}

}

?>